/**
 * Date: 11-11-24
 * Time: 上午10:56
 */
(function($){

    	$.Float=function(options){
    		var opt=$.extend(true,{},$.Float.settings,options);
    		var	float=new Float(opt);
    			float.init();

    	}
    	var Float=function(options){
    		this.o = options;
            this.tmplate=this.o.tmplate;
            this.style=this.o.style;
            this.cont=this.o.cont;
    		this.$float=$(this.tmplate);
            this.width=0;
            this.height=0;
            this.dur={bLeft:true,bDown:true};
            this.timeID=0;

    	}
    	Float.prototype={
    		init:function(obj){
                var _this=this;
    			this.$float.css(this.style).append(this.cont).appendTo('body');
                this.width=this.$float.outerWidth();
                this.height=this.$float.outerHeight();
                var $close=$('<div>关闭</div>').css({
                    width: 24,
                    height: 20,
                    position: 'absolute',
                    right:0,
                    fontSize:12,
                    top:0,
                    cursor:'pointer',
                    background:'#efefef'
                })
                this.$float.append($close)
                this.move();
                this.$float.bind('mouseover',function(){
                   clearTimeout(_this.timeID);
                }).bind('mouseout',function(){
                   _this.move();
                });
                $close.bind('click',function(){
                    _this.$float.remove();
                })


    		},
            getScroll:function(){
                return {
                    left: document.documentElement.scrollLeft || document.body.scrollLeft,
                    top: document.documentElement.scrollTop || document.body.scrollTop,
                    width:document.documentElement.clientWidth,
                    height:document.documentElement.clientHeight
                }
            },
            move:function(){
               var _this=this;
               var scroll=_this.getScroll();
               var iLeft=parseInt(_this.$float.css('left'),10)+scroll.left-_this.$float.data('scrollleft');
               var iTop=parseInt(_this.$float.css('top'),10)+scroll.top-_this.$float.data('scrolltop');
                    if(iTop<=scroll.top){
                        this.dur.bDown=true;
                    }else if(_this.height+iTop>=scroll.height+scroll.top){
                        this.dur.bDown=false;
                    }
                     if(iLeft<=scroll.left){
                        this.dur.bLeft=true;
                    }else if(_this.width+iLeft>=scroll.width+scroll.left){
                        this.dur.bLeft=false;
                    }
                    this.dur.bDown?iTop++:iTop--;
                    this.dur.bLeft?iLeft++:iLeft--;
                 this.$float.css({left:iLeft,top:iTop});
                _this.$float.data('scrollleft',scroll.left);
                _this.$float.data('scrolltop',scroll.top);
                _this.timeID=setTimeout(function(){
                    _this.move();
                },10);
            }


    	}


    	$.Float.settings={
            tmplate:'<div></div>',
            style:{
                width: 100,
                height: 100,
                position: 'absolute',
                left:0,
                top:0,
                background:'#ccc'
            },
            cont:''
    	}
    })(jQuery);